//
//  RealNameAuthViewController.m
//  XTXOneToOne
//
//  Created by Canny on 2018/10/29.
//  Copyright © 2018年 WK. All rights reserved.
//

#import "RealNameAuthViewController.h"
#import "RealNameAuthModel.h"
#import "ReSetRealNameAuthViewController.h"

@interface RealNameAuthViewController ()

@property (nonatomic,strong)UIView * bgView1;
@property (nonatomic,strong)UIView * bgView2;

@property (nonatomic,strong)UITextField * textField1;
@property (nonatomic,strong)UITextField * textField2;

@property (nonatomic,strong)RealNameAuthModel * AuthModel;


@end

@implementation RealNameAuthViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self setProperty];
    
    
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarHidden = NO;
    
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBarHidden=YES;
    
    if ([_Status isEqualToString:@"UI_1"]) {
        [self createUI_2];
    }else{
        [self loadData];
    }
    
    
}

-(void)setProperty{
    
    self.view.backgroundColor=RGB(255, 255, 255);
    
    [UIApplication sharedApplication].statusBarHidden = NO;
    
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBarHidden=YES;
    
    self.baseVcTitle = @"实名认证";
    
}

#pragma mark-未提交过-----
-(void)createUI_1{
    
    [_bgView1 removeFromSuperview];

    _bgView1 = [[UIView alloc]init];
    [self.bgView addSubview:_bgView1];
    [_bgView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(kStatusBarAndNavigationBarHeight);
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_bottom);
    }];
    
    UILabel *label = [[UILabel alloc] init];
    label.text = @"为了您的资金安全，需要验证您的身份证才可以进行其他操作，认证信息一经验证不能修改，请务必如实填写。";
    label.font = [UIFont systemFontOfSize:12];
    label.numberOfLines = 0;
    label.textColor = RGB(158, 169, 181);
    label.textAlignment=NSTextAlignmentLeft;
    [_bgView1 addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView1.mas_top).offset(30);
        make.left.equalTo(self.bgView1.mas_left).offset(15);
        make.width.equalTo(@(G_SCREENWIDTH-30));
    }];
    
    [self setTitle:@"真实姓名" andIndex:1 andPlaceholder:@"请输入您的真实姓名" andKeyboardType:UIKeyboardTypeNamePhonePad andSecureTextEntry:YES];
    [self setTitle:@"身份证号码" andIndex:2 andPlaceholder:@"请输入您的身份证号码" andKeyboardType:UIKeyboardTypeDefault andSecureTextEntry:YES];
 
    
    [self.rightBtn setTitle:@"确认" forState:UIControlStateNormal];
    [self.rightBtn setTitleColor:RGB(99, 139, 219) forState:UIControlStateNormal];
    
    
}


-(void)setTitle:(NSString *)title andIndex:(NSInteger)index andPlaceholder:(NSString *)Placeholder andKeyboardType:(UIKeyboardType)type andSecureTextEntry:(BOOL)isSecureTextEntry{
    
    CGFloat H =95,T=(index-1)*95+100;
    
    UIView * bgview = [[UIView alloc]init];
    bgview.frame = CGRectMake(0, T, G_SCREENWIDTH, H);
    [self.bgView1 addSubview:bgview];
    
    UILabel *label = [[UILabel alloc] init];
    label.text = title;
    label.font = [UIFont systemFontOfSize:12];
    label.textColor = RGB(30, 63, 89);
    label.textAlignment=NSTextAlignmentLeft;
    [bgview addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bgview.mas_top).offset(20);
        make.left.equalTo(bgview.mas_left).offset(15);
        make.width.equalTo(@(120));
        make.height.equalTo(@(30));
    }];
    
    UITextField * textField=[[UITextField alloc]init];
//    textField.secureTextEntry = isSecureTextEntry;
    textField.tag=500+index;
    [bgview addSubview:textField];
    [self setTextField:textField WithPlaceholder:Placeholder  and:type];
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(bgview.mas_bottom);
        make.left.equalTo(bgview.mas_left).offset(15);
        make.right.equalTo(bgview.mas_right).offset(-15);
        make.height.equalTo(@(50));
    }];
    if (index==1) {
        _textField1=textField;
    }else if (index==2){
        _textField2=textField;
    }
    
    
    UIView * line = [[UIView alloc]init];
    [bgview addSubview:line];
    line.backgroundColor = RGB(231, 235, 238);
    [bgview addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(bgview.mas_bottom).offset(-1);
        make.left.equalTo(bgview.mas_left).offset(15);
        make.right.equalTo(bgview.mas_right).offset(-15);
        make.height.equalTo(@(1));
    }];
    
    
}


-(void)setTextField:(UITextField*)textField WithPlaceholder:(NSString *)placeholder and:(UIKeyboardType)type  {
    
    
    textField.placeholder=placeholder;
    textField.borderStyle = UITextBorderStyleNone;
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    
//、    userNameTF.delegate = self;
    textField.autocorrectionType = UITextAutocorrectionTypeNo;
    textField.backgroundColor=[UIColor clearColor];
    textField.font = [UIFont systemFontOfSize:15];
    textField.textColor = RGB(40, 40, 40);
    textField.textAlignment = NSTextAlignmentLeft;
//    textField.returnKeyType = UIReturnKeyDefault;
//    textField.keyboardType = type;
    [textField setValue:RGB(190, 190, 190) forKeyPath:@"_placeholderLabel.textColor"];
    [textField setValue:[UIFont systemFontOfSize:14] forKeyPath:@"_placeholderLabel.font"];
    [textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:(UIControlEventEditingChanged)];
    
}

-(void)textFieldDidChange:(UITextField *)textField{
    textField.text= [textField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    if (textField.tag==501) {
        
    }else if(textField.tag==502){
        
    }
}


#pragma mark-确认
-(void)rightAction{
    
    if (_textField1.text.length<1) {
        [HUD showErrorMessage:@"请输入真实姓名！"];
        return;
    }
    
    if (_textField2.text.length<1) {
        [HUD showErrorMessage:@"请输入身份证号码！"];
        return;
    }
    
    
    NSString * URL = [NSString stringWithFormat:@"%@kyc/lv1",DEFAULTSEVERBASEURL];
    __weak __typeof(self)wself = self;
    
    [MOLoadHttpsData PostHttpDataWithUrlStr:URL Dic:@{@"true_name":_textField1.text,@"certificate_type":@"1",@"identification_number":_textField2.text,@"access_token":[LoginModel GetValue:DL_token]} SuccessBlock:^(id  _Nonnull responseObject) {
        NSDictionary * dic = responseObject;
        NSString * code = [NSString stringWithFormat:@"%@",dic[@"code"]];
        if ([code isEqualToString:@"0"]) {
            
            [HUD showErrorMessage:dic[@"msg"]];
            [wself.navigationController popViewControllerAnimated:YES];
            
        }else{
            [HUD showErrorMessage:dic[@"msg"]];
        }
        
    } FailureBlock:^(id  _Nonnull error) {
        
    }];
    
}

-(void)loadData{
    
    NSString * URL = [NSString stringWithFormat:@"%@kyc/findLv1",DEFAULTSEVERBASEURL];
    __weak __typeof(self)wself = self;
    NSDictionary * Dic = @{
                           @"access_token":[LoginModel GetValue:DL_token]
                           };
    
    [HUD showProgress:@"" inView:self.view];
    [MOLoadHttpsData GetHttpDataWithUrlStr:URL Dic:Dic SuccessBlock:^(id  _Nonnull responseObject) {
        
        NSDictionary * dic = responseObject;
        
        NSString * code = [NSString stringWithFormat:@"%@",dic[@"code"]];
        if ([code isEqualToString:@"0"]) {
            
            NSDictionary * dicc = dic[@"data"];
            wself.AuthModel = [RealNameAuthModel mj_objectWithKeyValues:dicc];
            [wself createUI_2];
            
        }else{
            [wself createUI_1];
        }
        
        [HUD dismissInView:self.view];
    } FailureBlock:^(id  _Nonnull error) {
    
        
        [HUD dismissInView:self.view];
        
    }];
    
    
    
}



#pragma mark-已经提交过了--------
-(void)createUI_2{
    
    [_bgView1 removeFromSuperview];
    _bgView1 = [[UIView alloc]init];
    [self.bgView addSubview:_bgView1];
    [_bgView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(kStatusBarAndNavigationBarHeight);
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_bottom);
    }];
    
    UILabel *label = [[UILabel alloc] init];
    label.font = [UIFont systemFontOfSize:30];
    label.textColor = RGB(30, 63, 89);
    label.textAlignment=NSTextAlignmentLeft;
    [self.bgView1 addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView1.mas_top).offset(10);
        make.left.equalTo(self.bgView1.mas_left).offset(15);
        make.width.equalTo(@(200));
        make.height.equalTo(@(40));
    }];
    
    UIView * bgView = [[UIView alloc]init];
    [self.bgView1 addSubview:bgView];
    bgView.backgroundColor=RGB(250, 250, 250);
    bgView.layer.shadowOpacity = 0.5;// 阴影透明度
    bgView.layer.shadowColor = [UIColor grayColor].CGColor;// 阴影的颜色
    bgView.layer.shadowRadius = 3;// 阴影扩散的范围控制
//    bgView.layer.shadowOffset = CGSizeMake(1, 1);// 阴影的范围
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
       make.top.equalTo(label.mas_bottom).offset(20);
        make.left.equalTo(self.bgView1.mas_left).offset(15);
        make.right.equalTo(self.bgView1.mas_right).offset(-15);
        make.height.equalTo(@(120));
    }];
    
    
    //@1：身份证；@2：护照
    if ([self.AuthModel.certificate_type integerValue]==1) {
        label.text=@"身份认证";
    }else if ([self.AuthModel.certificate_type integerValue]==2){
        label.text=@"护照认证";
    }
    
    UILabel *lab1 = [[UILabel alloc] init];
    lab1.font = [UIFont systemFontOfSize:15];
    lab1.textColor = RGB(30, 63, 89);
    lab1.textAlignment=NSTextAlignmentLeft;
    [bgView addSubview:lab1];
    lab1.text=@"实名认证";
    [lab1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bgView.mas_top).offset(10);
        make.left.equalTo(bgView.mas_left).offset(20);
        make.width.equalTo(@(200));
        make.height.equalTo(@(20));
    }];
    
    UILabel *lab2 = [[UILabel alloc] init];
    lab2.font = [UIFont systemFontOfSize:12];
    lab2.textColor = RGB(140, 159, 173);
    lab2.textAlignment=NSTextAlignmentLeft;
    [bgView addSubview:lab2];
    [lab2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lab1.mas_bottom).offset(5);
        make.left.equalTo(bgView.mas_left).offset(20);
        make.width.equalTo(@(200));
        make.height.equalTo(@(20));
    }];
    lab2.text=[NSString stringWithFormat:@"%@ | %@",self.AuthModel.true_name,self.AuthModel.identification_number];
    
    UILabel *lab3 = [[UILabel alloc] init];
    lab3.font = [UIFont systemFontOfSize:15];
    lab3.textColor = RGB(3, 191, 134);
    lab3.textAlignment=NSTextAlignmentLeft;
    [bgView addSubview:lab3];
    [lab3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(bgView.mas_bottom).offset(-20);
        make.left.equalTo(bgView.mas_left).offset(20);
        make.width.equalTo(@(200));
        make.height.equalTo(@(20));
    }];
    
    //@0：待审核；@1：审核通过；@2：审核拒绝
    if ([self.AuthModel.status integerValue]==0) {
        lab3.text = @"待审核";
    }else if ([self.AuthModel.status integerValue]==1){
        lab3.text = @"审核通过";
    }else if ([self.AuthModel.status integerValue]==2){
        lab3.text = @"审核拒绝";
        lab3.textColor = [UIColor redColor];
        
        
        UIButton *  againbtn = [[UIButton alloc]init];
        [bgView addSubview:againbtn];
        [againbtn setTitleColor:ThemeColor forState:UIControlStateNormal];
        [againbtn setTitle:@"编辑" forState:UIControlStateNormal];
        againbtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        againbtn.titleLabel.font = [UIFont systemFontOfSize: 15.0];
        [againbtn addTarget:self action:@selector(reSetClick) forControlEvents:UIControlEventTouchUpInside];
        [againbtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(lab3.mas_centerY).offset(0);
            make.right.equalTo(bgView.mas_right).offset(-10);
            make.width.mas_equalTo(50);
            make.height.mas_equalTo(30);
        }];
        
    }

  
    
}

-(void)reSetClick{
    
    ReSetRealNameAuthViewController * VC  = [[ReSetRealNameAuthViewController alloc]init];
    [self.navigationController pushViewController:VC animated:YES];
    
}

-(void)relayOutUI{
    
    
    
}



/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

